home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Games / SoundSprocketTest / TS3Window.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  8.0 KB  |  337 lines  |  [TEXT/MPS ]

  1. /*
  2.  *    File:        TS3Window.c
  3.  *
  4.  *    Copyright © 1996 Apple Computer, Inc.
  5.  */
  6.  
  7. #include <assert.h>
  8.  
  9. #include "TS3Window.h"
  10.  
  11. enum {
  12.     kWindowDataThumbprint = 'wdat'
  13. };
  14.  
  15.  
  16. typedef struct WindowData {
  17.     unsigned long        thumbprint;
  18.     void*                method[kWindowMethod_COUNT];
  19. } WindowData;
  20.  
  21.  
  22. static WindowData** Window_GetData(
  23.     WindowPtr            inWindow);
  24.  
  25. static WindowMethodPtr Window_GetMethod(
  26.     WindowPtr            inWindow,
  27.     WindowMethod        inMethod);
  28.  
  29.  
  30. /* =============================================================================
  31.  *        Window_Init (external)
  32.  *
  33.  *    Initializes our window stuff.
  34.  * ========================================================================== */
  35. void Window_Init(
  36.     void)
  37. {
  38. }
  39.  
  40.  
  41. /* =============================================================================
  42.  *        Window_Exit (external)
  43.  *
  44.  *    Cleans up.
  45.  * ========================================================================== */
  46. void Window_Exit(
  47.     void)
  48. {
  49. }
  50.  
  51.  
  52. /* =============================================================================
  53.  *        Window_GetData (internal)
  54.  *
  55.  *    Returns the WindowData record associated with this window.  Returns NULL
  56.  *    if the window doesn't have a WindowData record associated with it.
  57.  * ========================================================================== */
  58. WindowData** Window_GetData(
  59.     WindowPtr            inWindow)
  60. {
  61.     WindowData**        windowData;
  62.     
  63.     assert(inWindow != NULL);
  64.     
  65.     windowData = (WindowData**) GetWRefCon(inWindow);
  66.     
  67.     if (windowData != NULL && (*windowData)->thumbprint != kWindowDataThumbprint)
  68.     {
  69.         windowData = NULL;
  70.     }
  71.     
  72.     return windowData;
  73. }
  74.  
  75.  
  76. /* =============================================================================
  77.  *        Window_GetMethod (internal)
  78.  *
  79.  *    Returns the function pointer for the given method ID.  The result may be
  80.  *    NULL.
  81.  * ========================================================================== */
  82. WindowMethodPtr Window_GetMethod(
  83.     WindowPtr            inWindow,
  84.     WindowMethod        inMethod)
  85. {
  86.     WindowMethodPtr        result;
  87.     WindowData**        windowData;
  88.     
  89.     assert(inMethod >= kWindowMethod_FIRST);
  90.     assert(inMethod <  kWindowMethod_COUNT);
  91.     
  92.     result = NULL;
  93.     
  94.     windowData = Window_GetData(inWindow);
  95.     if (windowData != NULL)
  96.     {
  97.         result = (*windowData)->method[inMethod];
  98.     }
  99.     
  100.     return result;
  101. }
  102.  
  103.  
  104. /* =============================================================================
  105.  *        Window_New (external)
  106.  *
  107.  *    Makes a new WindowData structure for the window, points the window's refCon
  108.  *    at it, and fills it in using the metahandler.  The metahandler should return
  109.  *    a window method pointer that corresponds to the given method ID, or NULL
  110.  *    if none.
  111.  * ========================================================================== */
  112. void Window_New(
  113.     WindowPtr            inWindow,
  114.     WindowMethodPtr        (*inMetaHandler)(WindowMethod inMethod))
  115. {
  116.     WindowData**        windowData;
  117.     WindowMethod        method;
  118.     
  119.     assert(inWindow != NULL);
  120.     assert(inMetaHandler != NULL);
  121.     
  122.     // Allocate the window data
  123.     windowData = (WindowData**) NewHandle(sizeof(WindowData));
  124.     assert(windowData != NULL);
  125.     
  126.     // Fill it in
  127.     (*windowData)->thumbprint = kWindowDataThumbprint;
  128.     
  129.     for (method = kWindowMethod_FIRST; method < kWindowMethod_COUNT; method++)
  130.     {
  131.         (*windowData)->method[method] = (*inMetaHandler)(method);
  132.     }
  133.     
  134.     // Point the window refcon at it
  135.     SetWRefCon(inWindow, (long) windowData);
  136. }
  137.  
  138.  
  139. /* =============================================================================
  140.  *        Window_Dispose (external)
  141.  *
  142.  *    Disposes of the window's WindowData structure.
  143.  * ========================================================================== */
  144. void Window_Dispose(
  145.     WindowPtr            inWindow)
  146. {
  147.     WindowData**        windowData;
  148.     
  149.     windowData = Window_GetData(inWindow);
  150.     if (windowData != NULL)
  151.     {
  152.         DisposeHandle((Handle) windowData);
  153.         SetWRefCon(inWindow, 0);
  154.     }
  155. }
  156.  
  157.  
  158. /* =============================================================================
  159.  *        Window_IsMine (external)
  160.  *
  161.  *    Returns true if this is window has been set up with this stuff.
  162.  * ========================================================================== */
  163. Boolean Window_IsMine(
  164.     WindowPtr            inWindow)
  165. {
  166.     return Window_GetData(inWindow) != NULL;
  167. }
  168.  
  169.  
  170. /* =============================================================================
  171.  *        Window_GetSleep (external)
  172.  *
  173.  *    Sets *outSleep to the value that should be passed to WaitNextEvent when this
  174.  *    is the front window.
  175.  * ========================================================================== */
  176. void Window_GetSleep(
  177.     WindowPtr            inWindow,
  178.     UInt32*                outSleep)
  179. {
  180.     WindowMethodPtr        getSleepMethod;
  181.     
  182.     assert(outSleep != NULL);
  183.     
  184.     *outSleep = 0xFFFFFFFF;
  185.     
  186.     if (inWindow != NULL)
  187.     {
  188.         getSleepMethod = Window_GetMethod(inWindow, kWindowMethod_GetSleep);
  189.         if (getSleepMethod != NULL)
  190.         {
  191.             (*getSleepMethod)(inWindow, outSleep);
  192.         }
  193.     }
  194. }
  195.  
  196.  
  197. /* =============================================================================
  198.  *        Window_ConsumeEvent (external)
  199.  *
  200.  *    Sets *outConsumed to true if the window consumed the event, or false if not.
  201.  *    Overriding this method allows the window to do idle time things at null-
  202.  *    event times, or dialog things.
  203.  * ========================================================================== */
  204. void Window_ConsumeEvent(
  205.     WindowPtr            inWindow,
  206.     const EventRecord*    inEvent,
  207.     Boolean*            outConsumed)
  208. {
  209.     WindowMethodPtr        consumeEventMethod;
  210.     
  211.     assert(inEvent != NULL);
  212.     assert(outConsumed != NULL);
  213.     
  214.     *outConsumed = false;
  215.     
  216.     if (inWindow != NULL)
  217.     {
  218.         consumeEventMethod = Window_GetMethod(inWindow, kWindowMethod_ConsumeEvent);
  219.         if (consumeEventMethod != NULL)
  220.         {
  221.             (*consumeEventMethod)(inWindow, inEvent, outConsumed);
  222.         }
  223.     }
  224. }
  225.  
  226.  
  227. /* =============================================================================
  228.  *        Window_MouseDown (external)
  229.  *
  230.  *    Processes a mouse-down in the window content area.
  231.  * ========================================================================== */
  232. void Window_MouseDown(
  233.     WindowPtr            inWindow,
  234.     Point                inWhere)
  235. {
  236.     WindowMethodPtr        mouseDownMethod;
  237.     
  238.     if (inWindow != NULL)
  239.     {
  240.         mouseDownMethod = Window_GetMethod(inWindow, kWindowMethod_MouseDown);
  241.         if (mouseDownMethod != NULL)
  242.         {
  243.             (*mouseDownMethod)(inWindow, inWhere);
  244.         }
  245.     }
  246. }
  247.  
  248.  
  249. /* =============================================================================
  250.  *        Window_KeyDown (external)
  251.  *
  252.  *    Processes a key-down or auto-key event.
  253.  * ========================================================================== */
  254. void Window_KeyDown(
  255.     WindowPtr            inWindow,
  256.     char                inChar,
  257.     char                inKeyCap,
  258.     short                inModifiers,
  259.     Boolean                inAutoKey)
  260. {
  261.     WindowMethodPtr        keyDownMethod;
  262.     
  263.     if (inWindow != NULL)
  264.     {
  265.         keyDownMethod = Window_GetMethod(inWindow, kWindowMethod_KeyDown);
  266.         if (keyDownMethod != NULL)
  267.         {
  268.             (*keyDownMethod)(inWindow, inChar, inKeyCap, inModifiers, inAutoKey);
  269.         }
  270.     }
  271. }
  272.  
  273.  
  274. /* =============================================================================
  275.  *        Window_Update (external)
  276.  *
  277.  *    Redraws the contents of the window in response to an update event.
  278.  * ========================================================================== */
  279. void Window_Update(
  280.     WindowPtr            inWindow)
  281. {
  282.     WindowMethodPtr        updateMethod;
  283.     
  284.     if (inWindow != NULL)
  285.     {
  286.         updateMethod = Window_GetMethod(inWindow, kWindowMethod_Update);
  287.         if (updateMethod != NULL)
  288.         {
  289.             (*updateMethod)(inWindow);
  290.         }
  291.     }
  292. }
  293.  
  294.  
  295. /* =============================================================================
  296.  *        Window_Activate (external)
  297.  *
  298.  *    Handles window activation.
  299.  * ========================================================================== */
  300. void Window_Activate(
  301.     WindowPtr            inWindow)
  302. {
  303.     WindowMethodPtr        activateMethod;
  304.     
  305.     if (inWindow != NULL)
  306.     {
  307.         activateMethod = Window_GetMethod(inWindow, kWindowMethod_Activate);
  308.         if (activateMethod != NULL)
  309.         {
  310.             (*activateMethod)(inWindow);
  311.         }
  312.     }
  313. }
  314.  
  315.  
  316. /* =============================================================================
  317.  *        Window_Deactivate (external)
  318.  *
  319.  *    Handles window deactivation.
  320.  * ========================================================================== */
  321. void Window_Deactivate(
  322.     WindowPtr            inWindow)
  323. {
  324.     WindowMethodPtr        deactivateMethod;
  325.     
  326.     if (inWindow != NULL)
  327.     {
  328.         deactivateMethod = Window_GetMethod(inWindow, kWindowMethod_Deactivate);
  329.         if (deactivateMethod != NULL)
  330.         {
  331.             (*deactivateMethod)(inWindow);
  332.         }
  333.     }
  334. }
  335.  
  336.  
  337.